﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAO
{
    public class LoaiPhongDAO: AbtractDAO
    {
        public static List<LoaiPhongDTO> LayDSLoaiPhong()
        {
            OleDbConnection ketNoi = null;
            List<LoaiPhongDTO> dsLoaiPhong = new List<LoaiPhongDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaLoaiPhong,TenLoaiPhong,GiaTien,SoKhach FROM LOAIPHONG";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    LoaiPhongDTO LoaiPhong = new LoaiPhongDTO();
                    LoaiPhong.MaLoaiPhong = boLoc.GetInt32(0);

                    if (!boLoc.IsDBNull(1))
                    {
                        LoaiPhong.TenLoaiPhong = boLoc.GetString(1);
                    }

                    if (!boLoc.IsDBNull(2))
                    {
                        LoaiPhong.DonGia = boLoc.GetInt32(2);
                    }

                    if (!boLoc.IsDBNull(3))
                    {
                        LoaiPhong.SoKhach = boLoc.GetInt32(3);
                    }

                    dsLoaiPhong.Add(LoaiPhong);
                }
            }
            catch (Exception ex)
            {
                dsLoaiPhong = new List<LoaiPhongDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsLoaiPhong;
        }

        public static bool ThemLoaiPhong(string tenLoaiPhong, long donGia, long soKhach)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "INSERT INTO LOAIPHONG(TenLoaiPhong,GiaTien,SoKhach) VALUES(@TenLoaiPhong,@GiaTien,@TinhTrang)";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@TenPhong", OleDbType.VarChar);
                thamSo.Value = tenLoaiPhong;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@GiaTien", OleDbType.Integer);
                thamSo.Value = donGia;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@SoKhach", OleDbType.Integer);
                thamSo.Value = soKhach;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketQua = false;
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }

        public static void XoaLoaiPhong(long maLoaiPhong)
        {
            OleDbConnection ketNoi = null;

            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "DELETE FROM LOAIPHONG WHERE MaLoaiPhong=@MaLoaiPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@MaLoaiPhong", OleDbType.Integer);
                thamSo.Value = maLoaiPhong;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }

        public static void UpdateLoaiPhong(long maLoaiPhong, string tenLoaiPhong, long donGia, long soKhach)
        {
            OleDbConnection ketNoi = null;

            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "UPDATE LOAIPHONG SET TenLoaiPhong=@TenLoaiPhong,GiaTien=@GiaTien,SoKhach=@SoKhach WHERE MaLoaiPhong=@MaLoaiPhong";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;


                thamSo = new OleDbParameter("@TenLoaiPhong", OleDbType.VarChar);
                thamSo.Value = tenLoaiPhong;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@GiaTien", OleDbType.Integer);
                thamSo.Value = donGia;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@SoKhach", OleDbType.Integer);
                thamSo.Value = soKhach;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaLoaiPhong", OleDbType.Integer);
                thamSo.Value = maLoaiPhong;
                lenh.Parameters.Add(thamSo);


                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }
    }
}
